# -*- coding: utf-8 -*-
import uuid,json
import enum
from flask import current_app
from sqlalchemy import Column,String,Integer,Text,Binary,Boolean,DateTime,Enum
from adminApp.extensions import db, redis_store
from .base import BaseModel


# class TaskLogState(enum.Enum):
#     WaitReceive = u"等待接收"
#     Pushed = u"已推送"
#     Success = u"成功结果"
#     Error = u"失败结果"

# command_text command_type run_state result
class TaskLog(BaseModel, db.Model):
    '''设备执行命令记录'''
    # 是否系统自动生成的任务
    is_sytem_build=Column(Boolean, default=False)
    # 要执行的命令
    command_text = Column(Text)
    # 命令类型
    command_type = Column(String(34), default='mark')
    # 命令执行状态 等待接收 已推送 已返回结果
    run_state = Column(String(32), default="wait")
    # 命令执行结果
    result = Column(Text)

    machine_id = db.Column(db.Integer, db.ForeignKey('machine.id', ondelete='CASCADE'), nullable=False, index=True)
    machine = db.relationship('Machine', back_populates='task_logs')

    def task_to_redis(self):
        self.run_state = 'wait_exec'
        result = dict(id=self.id, command_text=self.command_text, command_type=self.command_type, run_state=self.run_state)
        redis_store.rpush(current_app.config.get('RD_TASK').format(self.machine.system_uuid), json.dumps(result))
        # db.session.commit()